package hivi.vn.hivigold.activities;

import hivi.vn.hivigold.R;
import hivi.vn.hivigold.adapters.BuyAdapter;
import hivi.vn.hivigold.adapters.HistoryAdapter;
import hivi.vn.hivigold.database.WebService;
import hivi.vn.hivigold.objects.Employee;
import hivi.vn.hivigold.objects.HistoryMaster;
import hivi.vn.hivigold.objects.Partner;
import hivi.vn.hivigold.objects.Report;
import hivi.vn.hivigold.objects.ReportFilter;
import hivi.vn.hivigold.objects.Stock;
import hivi.vn.hivigold.utils.Formats;

import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ReportActivity extends Activity {
	int CURRENT_REPORT = -1;
	public static final int REPORT_NXT = 1;
	public static final int REPORT_BAN = 2;
	public static final int REPORT_MUA = 3;
	int flagDate = -1;
	static final int DFROM = 1;
	static final int DTO = 2;
	Button btnExport;
	TextView txtDfrom;
	TextView txtDto;
	TextView txtVtID;
	TextView txtNvID;
	TextView txtKhID;
	TextView txtKhoID;
	TextView txtNhID;
	ListView lstReport;
	TextView txtTotal;
	
	/*TextView txtTotal_Sl_dt;
	TextView txtTotal_Tien_ban_dt;
	TextView txtTotal_Tien_von_dt;
	TextView txtTotal_Lai_dt;
	TextView txtTotal_Sl_m;
	TextView txtTotal_Tien_m;
	
	TextView txtTotal_ton_dau;
	TextView txtTotal_au_td;
	TextView txtTotal_du_dau;
	TextView txtTotal_sl_nhap;
	TextView txtTotal_au_nhap;
	TextView txtTotal_tien_nhap;
	TextView txtTotal_sl_xuat;
	TextView txtTotal_au_xuat;
	TextView txtTotal_tien_xuat;
	TextView txtTotal_ton_cuoi;
	TextView txtTotal_au_tc;
	TextView txtTotal_du_cuoi;
	
	LinearLayout lnlBuy;
	LinearLayout lnlSale;
	LinearLayout lnlNXT;
	LinearLayout lnlTotalBuy;
	LinearLayout lnlTotalSale;
	LinearLayout lnlTotalNXT;*/

	int mYear;
	int mMonth;
	int mDay;
	Date dFrom;
	Date dTo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.report_dt_layout);
		btnExport = (Button) findViewById(R.id.btnExport);
		txtDfrom = (EditText) findViewById(R.id.txtDFrom);
		txtDto = (EditText) findViewById(R.id.txtDTo);
		txtVtID = (EditText) findViewById(R.id.txtVtID);
		txtNhID = (EditText) findViewById(R.id.txtNhomID);
		txtNvID = (EditText) findViewById(R.id.txtNvID);
		txtKhID = (EditText) findViewById(R.id.tvKHID);
		txtKhoID = (EditText) findViewById(R.id.txtKhoID);
		lstReport = (ListView) findViewById(R.id.lst_history);
		txtTotal = (TextView) findViewById(R.id.tvTotal);
		
		dFrom = Formats.CurrentDate();
		dTo = Formats.CurrentDate();
		txtDfrom.setText(Formats.DateFormatDisplay(dFrom));
		txtDto.setText(Formats.DateFormatDisplay(dTo));
		
		btnExport.setOnClickListener(onClick);
		txtDfrom.setOnClickListener(onClick);
		txtDto.setOnClickListener(onClick);
		txtKhoID.setOnLongClickListener(onLong);
		txtNvID.setOnLongClickListener(onLong);
	}

	DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
		@Override
		public void onDateSet(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			// TODO Auto-generated method stub
			if (flagDate == DFROM) {
				dFrom = Formats.createDate(year, monthOfYear, dayOfMonth);
				txtDfrom.setText(Formats.DateFormatDisplay(dFrom));
			} else if (flagDate == DTO) {
				dTo = Formats.createDate(year, monthOfYear, dayOfMonth);
				txtDto.setText(Formats.DateFormatDisplay(dTo));
			}
			flagDate = -1;
		}
	};

	OnClickListener onClick = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			if (arg0.getId() == R.id.txtDFrom) {
				flagDate = DFROM;
				DatePickerDialog mDatePickerDialog = new DatePickerDialog(
						ReportActivity.this, mDateSetListener,
						Formats.getComDateNow(dFrom, "yyyy"),
						(Formats.getComDateNow(dFrom, "MM") - 1),
						Formats.getComDateNow(dFrom, "dd"));
				mDatePickerDialog.setTitle(R.string.dfrom);
				mDatePickerDialog.show();
			} else if (arg0.getId() == R.id.txtDTo) {
				flagDate = DTO;
				DatePickerDialog mDatePickerDialog = new DatePickerDialog(
						ReportActivity.this, mDateSetListener,
						Formats.getComDateNow(dTo, "yyyy"),
						(Formats.getComDateNow(dTo, "MM") - 1),
						Formats.getComDateNow(dTo, "dd"));
				mDatePickerDialog.setTitle(R.string.dto);
				mDatePickerDialog.show();
			} else if (arg0.getId() == R.id.btnExport) {
				export();
			}
		}
	};

	private void export()
	{
		if (txtDfrom.getText().toString().trim().length() == 0
				|| txtDto.getText().toString().trim().length() == 0) {
			Toast.makeText(getBaseContext(), R.string.warning_notempty,
					Toast.LENGTH_LONG).show();
			return;
		}
		ReportFilter filter = new ReportFilter();
		filter.setDfrom(dFrom);
		filter.setDto(dTo);
		filter.setEmployee(txtNvID.getText().toString().trim());
		filter.setGroup(txtNhID.getText().toString().trim());
		filter.setItem(txtVtID.getText().toString().trim());
		filter.setPartner(txtKhID.getText().toString().trim());
		filter.setStock(txtKhoID.getText().toString().trim());
		filter.setREPORTID(REPORT_BAN);
		//filter.setStatus(currentStatus);
		ReportTask history = new ReportTask();
		history.execute(filter);
	}
	
	private class ReportTask extends
			AsyncTask<ReportFilter, Void, ArrayList<Report>> {
		ProgressDialog dialog;
		int currentReport;

		@Override
		protected void onPostExecute(ArrayList<Report> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if (result != null && result != null) {
				BuyAdapter adapter = new BuyAdapter(getBaseContext(), result);
				lstReport.setAdapter(adapter);
				double total = 0;
				for (Report hm : result) {
					total = total+hm.getTien_ban();
				}
				txtTotal.setText(String.format(getResources().getString(R.string.total), Formats.money(total)));
			}else
			{
				txtTotal.setText(String.format(getResources().getString(R.string.total), Formats.money(0d)));
			}
			
			dialog.dismiss();
		}
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = new ProgressDialog(ReportActivity.this);
			dialog.setMessage(getResources().getString(R.string.alert_waiting));
			dialog.show();
		}

		@Override
		protected ArrayList<Report> doInBackground(ReportFilter... p) {
			// TODO Auto-generated method stub
			currentReport = p[0].getREPORTID();
			return WebService.getReport(getBaseContext(), p[0]);
		}
	}
	
	OnLongClickListener onLong = new OnLongClickListener() {

		@Override
		public boolean onLongClick(View v) {
			// TODO Auto-generated method stub
			if (v.getId() == R.id.txtNvID) {
				Intent i = new Intent(ReportActivity.this, SelectEmployee.class);
				startActivityForResult(i, SelectEmployee.SELECTED_EMPLOYEE_KEY);
			} else if (v.getId() == R.id.txtKhoID) {
				Intent i = new Intent(ReportActivity.this, SelectStock.class);
				startActivityForResult(i, SelectStock.SELECTED_STOCK_KEY);
			} else if (v.getId() == R.id.txtKhID)
			{
				Intent i = new Intent(ReportActivity.this, SelectPartner.class);
				startActivityForResult(i, SelectPartner.SELECTED_PARTNER_KEY);
			}
			return true;
		}
	};
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			switch (requestCode) {
			case SelectPartner.SELECTED_PARTNER_KEY:
				Partner p = (Partner) bundle
						.getSerializable(SelectPartner.SELECTED_PARTNER);
				txtKhID.setText(p.getPartnerID());
				break;
			case SelectEmployee.SELECTED_EMPLOYEE_KEY:
				Employee e = (Employee) bundle
						.getSerializable(SelectEmployee.SELECTED_EMPLOYEE);
				txtNvID.setText(e.getEmployeeID());
				break;
			case SelectStock.SELECTED_STOCK_KEY:
				Stock s = (Stock) bundle
						.getSerializable(SelectStock.SELECTED_STOCK);
				txtKhoID.setText(s.getStockID());
				break;
			}
		}
	};
	public static void onStartIntent(Context c) {
		Intent i = new Intent(c, ReportActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		c.startActivity(i);
	}
}



